	var flag; //判断走的是保存还是修改方法				
    $(function(){    
		$('#dg').datagrid({
			idField : 'id',
			loadMsg : '数据加载中请稍后……',
			fit : true,
			url : 'userAction_findUsers.action',
			lines : true,
			animate : true,
			nowrap : false,
			collapsible : true,
			fitColumns : true,
			striped : true, //隔行变色特性F
			rownumbers : true,
			remoteSort : false, //将本地排序去掉
			sortName : 'createTime',//设置排序列
			sortOrder : 'desc',
			method : 'get',
			toolbar : '#dt',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20, 50 ]		
		});

		$('#btncancer').click(function() {
			$('#dg_user').dialog('close');
		});
		
		//部门下拉框
		$('#dept').combobox({
					url:'departmentAction_departmentsToCombobox.action',
					valueField:'id',   
					textField:'deptName',
					editable:false
		});
		
		//职位下拉框
		$('#posi').combobox({
					url:'positionAction_positionsToCombobox.action',
					valueField:'id',   
					textField:'positionName',
					editable:false
		});		
		
		//增加或修改表单确定提交按钮
		$('#btnsummit').click(function(){
		alert($('#userForm').find('input[name=departmentID]').val());
				//后台保存
			if($('#userForm').form('validate')){
				$.ajax({
					type:'post',
					url: flag=='add'?'userAction_saveUser.action':'userAction_updateUser.action',
					cache:false,
					dataType:'json',
					contentType: 'application/x-www-form-urlencoded; charset=utf-8',  
					data:{
						id:$('#userForm').find('input[name=id]').val(),					
						employNo:$('#userForm').find('input[name=employNo]').val(),
						employName:$('#userForm').find('input[name=employName]').val(),
						password:$('#userForm').find('input[name=password]').val(),
						sex:$('#userForm').find('input[name=sex]').val(),
						birthday:$('#userForm').find('input[name=birthday]').val(),
						telephone:$('#userForm').find('input[name=telephone]').val(),
						email:$('#userForm').find('input[name=email]').val(),
						address:$('#userForm').find('input[name=address]').val(),
						departmentID:$('#userForm').find('input[name=departmentID]').val(),
						positionID:$('#userForm').find('input[name=positionID]').val()
					},
					success:function(result){
						if(result){
							//关闭窗口
							$('#dg_user').dialog('close');	
							//刷新节点 : 刷新当前选中节点		
							$('#dg').datagrid('reload');
							$.messager.show({
								title:'提示信息',
								msg:'增加或修改用户操作成功！',
								showType:'fade',
								style:{								
									right:'',
									bottom:''
								}
							});
						}else{
							$.messager.show({
								title:'提示信息',
								msg:'增加或修改用户操作失败！',
								showType:'fade',
								style:{								
									right:'',
									bottom:''
								}
							});
						}			
					}
				});								
			} else {				
			$.messager.show({
			title:'提示信息!' ,
			msg:'数据验证不通过,不能保存!',
			showType:'fade',
			style:{								
					right:'',
					bottom:''
				}
			});
		}							
	});
			
});

	function onAdd() {
		flag = 'add';
		//清空表单数据
		$('#dg').datagrid('unselectAll');
		$('#userForm').form('clear');
		$('#dg_user').dialog({
			title : '新增用户'
		});
		$('#dg_user').dialog('open');
	}

	function onUpdate() {
		flag = 'edit';
		var arr = $('#dg').datagrid('getSelections');
		if (arr.length != 1) {
			$('#dg').datagrid('unselectAll');
			$.messager.show({
				title : '提示信息!',
				msg : '只能选择单个用户编辑!',
				showType:'fade',
						style:{								
						right:'',
						bottom:''
					}
			});
		} else {
			$('#dg_user').dialog({
				title : '修改用户'
			});
			$('#userform').form('clear');
			//填充表单回显数据
			var node = $('#dg').datagrid('getSelected');
			$('#userForm').form('load', {
				id : node.id,
				employNo : node.employNo,
				employName : node.employName,
				password : node.password,
				sex: node.sex,
				birthday: node.birthday,
				telephone : node.telephone,
				email : node.email,
				address: node.address,
				departmentID : node.departmentID,
				positionID : node.positionID				
			});
			$('#dg_user').dialog('open');
		}
	}

	function onDelete() {
		var arr = $('#dg').datagrid('getSelections');
		if (arr.length <= 0) {
			$.messager.show({
				title : '提示信息!',
				msg : '请选择用户!',
				showType:'fade',
						style:{								
						right:'',
						bottom:''
					}
			});
		} else {
			$.messager.confirm("提示信息", "确认删除?", function(r) {
				if (r) {
					var ids = '';
					for ( var i = 0; i < arr.length; i++) {
						ids += arr[i].id + ',';
					}
					ids = ids.substring(0, ids.length - 1);
					$.post('userAction_deleteUsers.action', {
						ids : ids
					}, function(result) {
						if (result=="ok") {
							$('#dg').datagrid('unselectAll');
							$('#dg').datagrid('reload');
							$.messager.show({
								title : '提示信息',
								msg : '删除用户成功!',
								showType:'fade',
								style:{								
								right:'',
								bottom:''
								}
							});
						} else {
							$.messager.show({
								title : '提示信息',
								msg : '删除用户失败!',
								showType:'fade',
								style:{								
								right:'',
								bottom:''
								}
							});
						}

					});
				} else {
					return;
				}
			});
		}
	}
	function reject() {
		$('#dg').datagrid('unselectAll');
	}
	function openSearch() {
		$('#lay').layout('expand', 'north');

	}
	function searchUser() {

		$('#dg').datagrid('load', serializeForm($('#searchFrom')));

	}

	function clearForm() {
		$('#searchFrom').form('clear');
		$('#dg').datagrid('load', {});

	}

	function readExcel() {

		$('#rfileForm').form('clear');
		$('#readFile').dialog('open');
	}
	
	function authority() {
		var arr = $('#dg').datagrid('getSelections');
		if (arr.length != 1) {
			$('#dg').datagrid('unselectAll');
			$.messager.show({
				title : '提示信息!',
				msg : '只能选择单个用户授权!',
				showType:'fade',
						style:{								
						right:'',
						bottom:''
					}
			});
		} else {
			$('#userAuthority').dialog('open');
			var uid=arr[0].id;
			alert(uid);
			$("#tt").tree({
    			url: 'authorityAction_getAllForTree.action',
    			method:'get',
 				animate:true,
 				checkbox:true,
 				cascadeCheck:false,
 				lines:true,
        		onLoadSuccess:function(node,data){
        			$.post('userAuthorityAction_getAllUserAuthority.action', {
						uid:uid
						}, function(result) {
						 $.each(JSON.parse(result),function(i, obj){
                   			 var n = $("#tt").tree('find',obj.id);
                    		if(n){
                       		 $("#tt").tree('check',n.target);
                    		}
                		 });
					});
					$('#tt').tree('expandAll');
    			}
    		});		  			
			
		}
	}
	
	//获得选中权限并提交
	function getChecked() {
		var nodes = $('#tt').tree('getChecked');//获取权限树
		var node = $('#dg').datagrid('getSelections')[0];//获取选用户
		var aids = '';
		for ( var i = 0; i < nodes.length; i++) {
			if (aids != '')
				aids += ',';
			aids += nodes[i].id;
		}
		$.post('userAuthorityAction_addUserAuthority.action', {aids:aids,uid:node.id},function(result){
		$('#userAuthority').dialog('close');
			   if (result.status=="ok") {
					$('#tg').treegrid('loadData', result);
					$.messager.show({
						title : '提示信息',
						msg : '组授权成功',
						showType : 'fade',
						style : {
							right : '',
							bottom : ''
						}
					});
				} else {
					$.messager.show({
						title : '提示信息',
						msg : '组授权失败',
						showType : 'fade',
						style : {
							right : '',
							bottom : ''
						}
					});
				}
			}, 'json');
		 
	}

	function cancel(){
		$('#userAuthority').dialog('close');
	}
	//格式化时间------------------start
	function myformatter(date) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		//var h = date.getHours();
		//var min = date.getMinutes();
		//var sec = date.getSeconds();
		//return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
		//		+ (d < 10 ? ('0' + d) : d) + ' ' + (h < 10 ? ('0' + h) : h)
		//		+ ':' + (min < 10 ? ('0' + min) : min) + ':'
		//		+ (sec < 10 ? ('0' + sec) : sec);
		return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
				+ (d < 10 ? ('0' + d) : d);
	}
	function myparser(s) {
		if (!s)
			return new Date();
		var ss = (s.split('-'));
		var y = parseInt(ss[0], 10);
		var m = parseInt(ss[1], 10);
		var d = parseInt(ss[2], 10);
		//var h = s.substring(ss[3], 10);
		//var min = s.substring(ss[4], 10);
		//var sec = s.substring(ss[5], 10);
		//if (!isNaN(y) && !isNaN(m) && !isNaN(d) && !isNaN(h) && !isNaN(min)
		//		&& !isNaN(sec)) {return new Date(y, m - 1, d, h, min, sec);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
			return new Date(y, m - 1, d);
		} else {
			return new Date();
		}
	}
	//格式化时间------------------end
	//js方法：序列化表单 			
	function serializeForm(form) {
		var obj = {};
		$.each(form.serializeArray(), function(index) {
			if (obj[this['name']]) {
				obj[this['name']] = obj[this['name']] + ',' + this['value'];
			} else {
				obj[this['name']] = this['value'];
			}
		});
		return obj;
	}